package com.fastwire.carriere.hibernate.daoimpl;

import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.fastwire.carriere.common.Messages;
import com.fastwire.carriere.common.util.StringUtil;
import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.dao.UserDao;
import com.fastwire.carriere.user.entity.User;
import com.fastwire.carriere.user.entity.UserGroup;
import com.fastwire.carriere.user.vo.UserGroupVo;
import com.fastwire.carriere.user.vo.UserVo;

// TODO: Auto-generated Javadoc
/**
 * The Class UserDaoImpl.
 */
public class UserDaoImpl extends AbstractDAOImpl implements UserDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.dao.UserDao#getUserbyName(java.lang.String)
	 */
	@Override
	public User getUserbyName(String name) throws DAOException {

		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(User.class);
		detachedCriteria.add(Restrictions.eq("name", name));
		return (User) getHibernateTemplate().findSingleObjByCriteria(
				detachedCriteria, false, true);

	}

	@Override
	public List<User> getUsersbyUserGroup(UserGroup userGroup)
			throws DAOException {

		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(User.class);
		detachedCriteria.add(Restrictions.eq("userGroup", userGroup));
		return getHibernateTemplate().findObjsByCriteria(detachedCriteria,
				false, true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.UserDao#getUserGroupbyName(java.lang.String)
	 */
	@Override
	public UserGroup getUserGroupbyName(String name) throws DAOException {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(UserGroup.class);
		detachedCriteria.add(Restrictions.eq("name", name));
		return (UserGroup) getHibernateTemplate().findSingleObjByCriteria(
				detachedCriteria, false, true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.dao.UserDao#getUserGroupbyId(long)
	 */
	@Override
	public UserGroup getUserGroupbyId(long id) throws DAOException {

		return (UserGroup) find(UserGroup.class, id);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.UserDao#getUsersbySearchCriteria(com.fastwire
	 * .carriere.user.vo.UserVo)
	 */
	@Override
	public List<User> getUsersbySearchCriteria(UserVo userVo)
			throws DAOException {

		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(User.class);

		if (!StringUtil.isEmptyString(userVo.getName())) {

			detachedCriteria
					.add(getWildCardCriterion("name", userVo.getName()));
		}

		if (!StringUtil.isEmptyString(userVo.getEmail())) {

			detachedCriteria.add(getWildCardCriterion("email",
					userVo.getEmail()));
		}

		if (!StringUtil.isEmptyString(userVo.getUserGroup())) {
			detachedCriteria
					.setFetchMode("userGroup", FetchMode.JOIN)
					.createAlias("userGroup", "userGroup")
					.add(getWildCardCriterion("userGroup.name",
							userVo.getUserGroup()));

		}

		return getHibernateTemplate().findObjsByCriteria(detachedCriteria,
				false, true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.UserDao#getUserGroupsbySearchCriteria(com.fastwire
	 * .carriere.user.vo.UserGroupVo)
	 */
	@Override
	public List<UserGroup> getUserGroupsbySearchCriteria(UserGroupVo searchVo)
			throws DAOException {

		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(UserGroup.class);

		if (!StringUtil.isEmptyString(searchVo.getName())) {

			detachedCriteria.add(getWildCardCriterion("name",
					searchVo.getName()));
		}

		if (!StringUtil.isEmptyString(searchVo.getDescription())) {

			detachedCriteria.add(getWildCardCriterion("description",
					searchVo.getDescription()));
		}

		return getHibernateTemplate().findObjsByCriteria(detachedCriteria,
				false, true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.dao.UserDao#deleteUserbyId(long)
	 */
	@Override
	public void deleteUserbyId(long id) throws DAOException {
		Session session = HibernateUtil.openSession();
		try {

			session.beginTransaction();
			Object obj = session.get(User.class, id);
			if (obj != null) {
				session.delete(obj);
			} else {
				throw new DAOException(Messages.getString(
						"message.common.dao.idnotexist.exception",
						new String[] { String.valueOf(id) }));
			}
			session.getTransaction().commit();
		} catch (Exception ex) {
			if (session.getTransaction().isActive()) {
				session.getTransaction().rollback();
			}
			throw new DAOException(ex);
		} finally {
			HibernateUtil.close(session);

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.dao.UserDao#deleteUserGroupbyId(long)
	 */
	@Override
	public void deleteUserGroupbyId(long id) throws DAOException {
		Session session = HibernateUtil.openSession();
		try {

			session.beginTransaction();
			Object obj = session.get(UserGroup.class, id);
			if (obj != null)
				session.delete(obj);
			else {
				throw new DAOException(Messages.getString(
						"message.common.dao.idnotexist.exception",
						new String[] { String.valueOf(id) }));
			}
			session.getTransaction().commit();
		} catch (Exception ex) {
			if (session.getTransaction().isActive()) {
				session.getTransaction().rollback();
			}
			throw new DAOException(ex);
		} finally {
			HibernateUtil.close(session);

		}

	}

}
